package com.sntk.organauthomanager.domain.model;

import com.sntk.organauthomanager.infrastructure.exception.ObjectNotFoundException;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName: assignResourcesToRoleService
 * @author: wgchao
 * @createTime: 2020/7/20 4:18 PM
 * @Description: 为角色分配资源权限
 */
public class assignResourcesToRoleService {

    public RoleResource assign(Role role, List<Resource> resources){
        if(role == null){
            throw new ObjectNotFoundException("角色不存在");
        }
        role.normalException();
        Set<String> resourceSet = new HashSet<>();
        if(resources!=null && !resources.isEmpty()){
            for(Resource resource : resources){
                resource.normalException();
                resourceSet.add(resource.getMark());
            }
        }
        return new RoleResource(role.getRoleIdentify(), resourceSet);
    }
}
